(function ($) {
    var homePage = "First";
    var prevPage = "Prev";
    var nextPage = "Next";
    var endPage = "Last";
    $.fn.pager = function (options) {
        var opts = $.extend({}, $.fn.pager.defaults, options);
        return this.each(function () {
            var lang = options.lang;
            if (lang == 'zh-cn') {
                homePage = "首页";
                prevPage = "上一页";
                nextPage = "下一页";
                endPage = "末页";
            }

            $(this).empty().append(renderpager(parseInt(options.pagenumber), parseInt(options.pagecount), options.buttonClickCallback));
            //$('.pages li').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto";});
        });
    };

    function renderpager(pagenumber, pagecount, buttonClickCallback) {
        var $pager = $("<div class='page'></div>");
        $pager.append(renderButton(homePage, pagenumber, pagecount, buttonClickCallback)).append(renderButton(prevPage, pagenumber, pagecount, buttonClickCallback));
        var startPoint = 1;
        var endPoint = 9;
        var thpoint = "...";
        if (pagenumber > 4) {
            startPoint = pagenumber - 4;
            endPoint = pagenumber + 4;
        }
        if (endPoint > pagecount) {
            startPoint = pagecount - 8;
            endPoint = pagecount;
            thpoint = "";
        }
        if (pagecount == 9) {
            thpoint = "";
        }
        if (startPoint < 1) {
            startPoint = 1;
        }
        for (var page = startPoint; page <= endPoint; page++) {
            var currentButton = "";
            //var spanHtml = page;
            //currentButton.append(spanHtml);
            if (page == pagenumber) {
                currentButton = $("<span></span>");
                currentButton.append(page).addClass("current");
                //currentButton.append(spanHtml);
                currentButton.appendTo($pager);
            } else {
                currentButton = $("<a href='javascript:void(0);'></a>");
                currentButton.append(page);
                currentButton.click(function () {
                    buttonClickCallback($(this).html());
                });
                currentButton.appendTo($pager);
            }
        }
        $pager.append(thpoint).append(renderButton(nextPage, pagenumber, pagecount, buttonClickCallback)).append(renderButton(endPage, pagenumber, pagecount, buttonClickCallback));
        //$pager.append("<li class='thpoint' style='margin-top:3px;'>共: "+pagecount+" 页</li>");
        //var strgoto = $("<li class='thpoint'>当前<input type='text' value='"+pagenumber+"'maxlength='6' id='gotoval' style='width:20px; height:16px;margin-top:-3px;padding-top:2px;padding-left:10px;'/>页</li>");
        //$pager.append(strgoto);
        //$pager.append(changepage('go',pagecount,buttonClickCallback));
        return $pager;
    }

    function changepage(buttonLabel, pagecount, buttonClickCallback) {
        var $btngoto = $('<a href=#ucHeader>' + buttonLabel + '</a>');
        $btngoto.click(function () {
            var gotoval = $('#gotoval').val();
            var patrn = /^[1-9]{1,20}$/;
            if (!patrn.exec(gotoval)) {
                alert("请输入非零的正整数！");
                return false;
            }
            var intval = parseInt(gotoval);
            if (intval > pagecount) {
                alert("您输入的页面超过总页数 " + pagecount);
                return;
            }
            buttonClickCallback(intval);
        });
        return $btngoto;
    }

    function renderButton(buttonLabel, pagenumber, pagecount, buttonClickCallback) {
        var $Button = $("<span>" + buttonLabel + "</span>");
        var $Button1 = $("<a href='javascript:void(0);'>" + buttonLabel + "</a>");
        var destPage = 1;
        switch (buttonLabel) {
            case homePage:
                destPage = 1;
                break;
            case prevPage:
                destPage = pagenumber - 1;
                break;
            case nextPage:
                destPage = pagenumber + 1;
                break;
            case endPage:
                destPage = pagecount;
                break;
        }
        if (buttonLabel == homePage || buttonLabel == prevPage) {

            if (pagenumber <= 1) {
                $Button.addClass('disabled');
                return $Button;
            } else {
                $Button1.click(function () {
                    buttonClickCallback(destPage);
                });
                return $Button1;
            }
        }
        else {
            if (pagenumber >= pagecount) {
                $Button.addClass('disabled');
                return $Button;
            } else {
                $Button1.click(function () {
                    buttonClickCallback(destPage);
                });
                return $Button1;
            }
        }
    }

    $.fn.pager.defaults = {
        pagenumber: 1,
        pagecount: 1};
})(jQuery);